import math

import pandas as pd
from matplotlib import pyplot as plt

from ssa import Sparrow
from function import range_function

test_function = ['Schaffer','Michalewicz', 'Shubert', 'Griewank', 'Eggholder', 'Bukin', 'Schwefe']

if __name__ == '__main__':
    dim = 2  # 函数维度
    pq = 50  # 种群数量
    iter_time = 30  # 迭代次数
    test_nums = 30  # 实验次数
    threshold_value = 0.8  # 阈值
    disc_sparrow_prop = 0.2  # 发现者麻雀占比

    for f in range(0,len(test_function)):
        # 所有实验的数据储存变量
        b_X_l = []
        b_f_l = []
        i_n = []
        # 所有实验的数据储存变量

        # 进行实验
        print(range_function(test_function[f]))
        plt.figure(figsize=(6, 6))
        for i in range(test_nums):
            fu_prop = range_function(test_function[f])

            SSA = Sparrow(dim, pq, iter_time, threshold_value, disc_sparrow_prop, 0.1, fu_prop[0], fu_prop[1], fu_prop[2],
                          fu_prop[3], 1,
                          str(i))  # Michalewicz
            b_X, b_f = SSA.SSA_run()
            # 将所有实验的数据保存至CSV
            b_f_l.append(b_f)
            b_X_l.append(str(b_X))
            i_n.append(i + 1)
            print(b_X, b_f)
        dict = {'num': i_n, 'X_best': b_X_l, 'F_best': b_f_l}
        df = pd.DataFrame(dict)
        df.to_csv(test_function[f]+ '.csv')
        # 将所有实验的数据保存至CSV
